<?php
/**
 * 
 */
namespace app\member\controller;

use app\member\model\Article as ArticleModel;
use Request;

class Article extends Base
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index($act = "")
    {
        $where = [];
        if (Request::has('search')) {
            $where[] = ['title', 'like', '%' . Request::param('search/s', '', 'strip_tags') . '%'];
        }
        $list   = ArticleModel::order('id desc')->where($where)->paginate(20);
        $search = Request::param('search/s', '', 'strip_tags') ? Request::param('search/s', '', 'strip_tags') : "";
        $this->assign([
            'list'   => $list,
            'page'   => $list->render(),
            'search' => $search,
        ]);
        return $this->fetch();
    }

    public function show($id = '')
    {
        if (!empty($id)) {
            $info = ArticleModel::get($id);
            //上一篇
            $front      = ArticleModel::where('id', '<', $id)->where('typeid', '=', $info['typeid'])->order('id desc')->limit('1')->find();
            $page['pr'] = !$front ? '没有了' : '<a href="' . url('show', ['id' => $front['id']]) . '">' . $front['title'] . '</a>';

            //下一篇
            $after        = ArticleModel::where('id', '>', $id)->where('typeid', '=', $info['typeid'])->order('id desc')->limit('1')->find();
            $page['next'] = !$after ? '没有了' : '<a href="' . url('show', ['id' => $after['id']]) . '">' . $after['title'] . '</a>';
            $this->assign([
                'info' => $info,
                'page' => $page,
            ]);
            return $this->fetch();
        }
    }

}
